<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\cdc\append;

use app\enum\CommonEnum;
use app\model\mongo\MongoInfoModel;
use app\model\mongo\MongoStatModel;
use app\service\cdc\sync\VideoLogService;
use app\service\cdc\VideoService;

abstract class VideoAppendService extends AppendService
{
    /**
     * @var int 媒体ID
     */
    protected int $mediaId = 0;

    /**
     * 素材模型.
     */
    protected ?MongoInfoModel $infoModel;

    /**
     * 汇总模型.
     */
    protected ?MongoStatModel $statModel;

    /**
     * 初始化模型.
     */
    public function setModel(): bool
    {
        if (! $this->getInfoModel()) {
            return false;
        }

        if (! $this->getStatModel()) {
            return false;
        }
        return true;
    }

    /**
     * 汇总数据是否已存在.
     */
    public function isExistStat(): bool
    {
        return $this->statModel instanceof MongoStatModel && ! empty($this->statModel->getKey());
    }

    public function getInfo(array $where = [])
    {
        return static::getSyncModel()->where($where)->field(true)->find();
    }



    /**
     * 获取关系数据模型.
     * @return bool|mixed
     */
    protected function getInfoModel()
    {
        if ($this->mediaId == CommonEnum::KS_MEDIA) {
            $fields = ['user_id', 'video_id'];
        } elseif ($this->mediaId == CommonEnum::OE_MEDIA) {
            $fields = ['account_id', 'video_id'];
        } else {
            $fields = ['account_id', 'video_id'];
        }

        $where = [];
        foreach ($fields as $v) {
            if ($this->mediaId == CommonEnum::KS_MEDIA && $v == 'video_id') {
                $where['photo_id'] = $this->syncModel->getAttr($v);
            } else {
                $where[$v] = $this->syncModel->getAttr($v);
            }
        }

        return $this->infoModel = VideoService::getInfoModel($this->mediaId, 'video', $where);
    }

    /**
     * 获取关系数据模型.
     * @return bool|mixed
     */
    protected function getStatModel()
    {
        $fields = $this->syncModel->getStatField();
        $where = [];
        foreach ($fields as $v) {
            $where[$v] = $this->syncModel->getAttr($v);
        }

        return $this->statModel = VideoService::getStatModel($this->mediaId, $this->syncModel->getAttr('type'), $where);
    }

    /**
     * 追加统计数据.
     */
    protected function appendStat(): bool
    {
        if ($this->isExistStat()) {
            return VideoLogService::deleteLog($this->syncModel);
        }
        return $this->addStatByInfo();
    }

    /**
     * 添加统计数据.
     */
    protected function addStatByInfo(): bool
    {
        if (! VideoService::addStat($this->statModel, $this->getStatField())) {
            return self::setError('更新统计数据失败');
        }

        return VideoLogService::deleteLog($this->syncModel);
    }

    /**
     * 获取更新字段.
     */
    protected function getStatField(): array
    {
        $fields = $this->statModel->getNeedField();

        $data = [];
        foreach ($fields as $v) {
            $data[$v] = $this->syncModel->getAttr($v);
        }

        return $data;
    }
}
